package com.ahao.service;

import com.ahao.dao.TeacherDao;
import com.ahao.model.RespPageBean;
import com.ahao.model.Teacher;
import com.ahao.model.TeacherDTO;
import com.ahao.utils.CommonsUtils;
import com.ahao.utils.DBUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class TeacherService {
    TeacherDao teacherDao = new TeacherDao();

    /**
     * 分页查询所有教师
     *
     * @param page
     * @param rows
     * @param sort
     * @param order
     * @return
     */
    public RespPageBean getTeacherList(String page, String rows, String sort, String order) {
        if (page == null || page.equals("")) {
            page = "1";
        }
        if (rows == null || rows.equals("")) {
            rows = Integer.MAX_VALUE + "";
        }
        if (sort == null || sort.equals("")) {
            sort = "cid";
        }
        if (order == null || order.equals("")) {
            order = "asc";
        }
        List<TeacherDTO> list = null;
        Long total = null;
        try {
            total = teacherDao.getTotal();
            int offset = Integer.parseInt(page);
            int pageSize = Integer.parseInt(rows);
            list = teacherDao.getTeacherList((offset - 1) * pageSize, pageSize, sort, order);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return new RespPageBean(total, list);
    }

    /**
     * 添加教师
     *
     * @param number
     * @param name
     * @param gender
     * @param phone
     * @param qq
     * @param courses
     * @return
     */
    public Integer addTeacher(String number, String name, String gender, String phone, String qq, String[] courses) {
        Connection con = DBUtils.getConnection();
        try {
            Teacher teacher = teacherDao.getTeacherByNumber(number);
            //教职工号重复
            if (teacher != null) {
                return CommonsUtils.TABLE_DATA_REPEAT;
            }
            Teacher teacher1 = new Teacher();
            teacher1.setNumber(number);
            teacher1.setName(name);
            teacher1.setGender(gender);
            teacher1.setPhone(phone);
            teacher1.setQq(qq);
            con.setAutoCommit(false);
            Integer i = teacherDao.addTeacher(con, teacher1);
            Integer i2 = teacherDao.addTeacherCourses(teacher1, courses);
            con.commit();
            return i == 1 && i2 == courses.length ? CommonsUtils.UPDATE_SUCCESS : CommonsUtils.OTHER_EXCEPTION;
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            DBUtils.close(con);
        }
        return CommonsUtils.OTHER_EXCEPTION;
    }


    public Integer updateTeacher(TeacherDTO teacherDTO) {
        Connection con = DBUtils.getConnection();
        try {
            con.setAutoCommit(false);
            Integer i = teacherDao.deleteTeacherCourse(con, teacherDTO);
            Integer i2 = teacherDao.updateTeacher(con, teacherDTO);
            Integer i3 = teacherDao.addTeacherCourses(teacherDTO, teacherDTO.getCourse());
            con.commit();
            return i == 1 && i2 == 1 && i3 == teacherDTO.getCourse().length ? CommonsUtils.UPDATE_SUCCESS : CommonsUtils.OTHER_EXCEPTION;
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            DBUtils.close(con);
        }
        return CommonsUtils.OTHER_EXCEPTION;
    }
}
